package com.kkm.controller;

import com.kkm.pojo.Order;
import com.kkm.pojo.PageBean;
import com.kkm.service.Imp.OrderServiceImp;
import com.kkm.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    OrderService service=new OrderServiceImp();

    //展示订单
    public void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
        // req.removeAttribute("currPageMax");
        PageBean<Order> pageBeanOrder=service.selectAll(currentPage,rows);
        req.setAttribute("Order",pageBeanOrder);
        req.getRequestDispatcher("/showAllOrder.jsp").forward(req,resp);
    }
    //模糊查询订单
    public void checkOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取查询条件，封装成集合
        Map<String, String[]> condition = req.getParameterMap();

        for (String str : condition.keySet()) {
            System.out.println("map***" + str);
            System.out.println(condition.get(str)[0]);
        }
        String currentPage=req.getParameter("currentPage");

        int rows=8;
        req.removeAttribute("currPageMax");
        PageBean<Order> orderList=service.checkOrder(condition,currentPage,rows);
        if(orderList==null){
//            userList=service.showUser(currentPage,rows); 如果需要在查询不到时显示全部数据
            req.setAttribute("Order",orderList);
            req.getRequestDispatcher("/showAllOrder.jsp").forward(req,resp);
        }else {
            req.setAttribute("Order",orderList);
            req.getRequestDispatcher("/showAllOrder.jsp").forward(req,resp);
        }

    }
    //删除订单
    public void deleteOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int oid = Integer.parseInt(req.getParameter("oid"));
        service.deleteOrder(oid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Order> pageBeanOrder=service.selectAll(currentPage,rows);
        req.setAttribute("Order",pageBeanOrder);
        req.getRequestDispatcher("/showAllOrder.jsp").forward(req,resp);
    }
//    //修改用户

    //修改用户（第一步接收信息）
    public void orderById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int oid= Integer.parseInt(req.getParameter("oid"));
        Order order=service.orderById(oid);
        req.setAttribute("order",order);
        req.getRequestDispatcher("/modifyOrder.jsp").forward(req,resp);
    }

    public void modifyOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        int oid= Integer.parseInt(req.getParameter("oid"));
        String username=req.getParameter("username");
        String timeStr=req.getParameter("timeStr");

        String province=req.getParameter("province");
        String city=req.getParameter("city");
        String area =req.getParameter("area");
        String address=province+"-"+city+"-"+area;
        System.out.println(oid+" "+username+" "+timeStr);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date timeSt=sdf.parse(timeStr);
        System.out.println(timeSt);

        service.modifyOrder(address,oid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Order> pageBeanUser=service.selectAll(currentPage,rows);
        req.setAttribute("Order",pageBeanUser);
        req.getRequestDispatcher("/showAllOrder.jsp").forward(req,resp);
    }
}
